package com.cqc.flycotablayout01.adapter;

import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cqc.flycotablayout01.R;

/**
 * Created by cui on 2017/9/5.
 */

public class MainPagerAdapter extends PagerAdapter {
    String[] titles = {"语文", "数学", "英语","物理","化学","生物"};

    public MainPagerAdapter(String[] titles) {
        this.titles = titles;
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View rootView = LayoutInflater.from(container.getContext()).inflate(R.layout.layout_pager, container, false);
        TextView tv = (TextView) rootView.findViewById(R.id.tv);
        tv.setText("" + position);
        container.addView(rootView);
        switch (position) {
            case 0:
                rootView.setBackgroundColor(Color.RED);
                break;
            case 1:
                rootView.setBackgroundColor(Color.GREEN);
                break;
            case 2:
                rootView.setBackgroundColor(Color.YELLOW);
                break;
        }
        return rootView;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}
